package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallBrand;
import com.cskaoyan.bean.CskaoyanmallBrandExample;
import com.cskaoyan.bean.vo.WxBrandsListVo;
import com.cskaoyan.mapper.CskaoyanmallBrandMapper;
import com.cskaoyan.service.WxBrandServiceZGY;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class WxBrandServiceImplZGY implements WxBrandServiceZGY {

    @Autowired
    CskaoyanmallBrandMapper mapper;


    /**
     * 根据page，size查询brands
     */
    @Override
    public WxBrandsListVo queryBrands(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        CskaoyanmallBrandExample cskaoyanmallBrandExample = new CskaoyanmallBrandExample();

        List<CskaoyanmallBrand> brands = mapper.selectByExample(cskaoyanmallBrandExample);

//        对查询出来的brands做一个过滤，去除掉那些被逻辑删除的brand
//        TODO: 做一个工具类去重封装
        Iterator<CskaoyanmallBrand> iterator = brands.iterator();
        while (iterator.hasNext()) {
            Boolean deleted = iterator.next().getDeleted();
            if (deleted != null && deleted == true) {
                iterator.remove();
            }
        }

        PageInfo<CskaoyanmallBrand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();

        WxBrandsListVo wxBrandsListVo = new WxBrandsListVo();
        wxBrandsListVo.setTotalPages(total);
        wxBrandsListVo.setBrandList(brands);

        return wxBrandsListVo;
    }

    /**
     * 查询品牌详细信息
     */
    @Override
    public CskaoyanmallBrand queryBrandDetail(Integer id) {

        CskaoyanmallBrand brandDtetail = mapper.selectByPrimaryKey(id);

        // TODO: 没查到的处理情况

        return brandDtetail;
    }
}